/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bensoft.minesweeper.gui;

import com.bensoft.minesweeper.ApplicationController;
import com.bensoft.minesweeper.resources.FileResources;
import com.bensoft.minesweeper.resources.strings.StringResources;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

/**
 *
 * @author Ben Wolsieffer
 */
public class AboutDialog extends JDialog implements GUILoader {

    private JLabel aboutMessage;
    private JPanel bottomPanel;
    private JLabel titleMessage;
    private JLabel homepageUrlLabel;

    public AboutDialog() {
        super(ApplicationController.getMinesweeperFrame());
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    public void initializeComponents() {
        titleMessage = new JLabel(StringResources.getString("gui.dialog.about.label.title"), FileResources.getAppIcon(), SwingConstants.CENTER);
        aboutMessage = new JLabel(StringResources.getString("gui.dialog.about.label.message"));
        bottomPanel = new JPanel();
        homepageUrlLabel = new JLabel(StringResources.getString("gui.dialog.about.label.homepage"));
    }

    public void setLayouts() {
        setLayout(new BorderLayout());
        bottomPanel.setLayout(new BorderLayout(0, 10));
    }

    public void addComponents() {
        add(titleMessage, BorderLayout.NORTH);
        bottomPanel.add(new JSeparator(), BorderLayout.NORTH);
        bottomPanel.add(aboutMessage, BorderLayout.CENTER);
        bottomPanel.add(homepageUrlLabel, BorderLayout.SOUTH);
        add(bottomPanel, BorderLayout.CENTER);

    }

    public void addComponentListeners() {
        homepageUrlLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isBrowsingSupported()) {
                    try {
                        Desktop.getDesktop().browse(new URI("http://code.google.com/p/minesweeper-java/"));
                    } catch (Exception ex) {
                    }
                }
            }
        });
    }

    public void otherInit() {
        //setIconImage(null);
        setTitle(StringResources.getString("gui.dialog.about.title"));
        getRootPane().setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        setResizable(false);
        setModal(true);
        pack();
        //setLocationRelativeTo(ApplicationController.getMinesweeperFrame());
    }

    private boolean isBrowsingSupported() {
        if (!Desktop.isDesktopSupported()) {
            return false;
        }
        boolean result = false;
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            result = true;
        }
        return result;

    }

    @Override
    public void setVisible(boolean b) {
        setLocationRelativeTo(ApplicationController.getMinesweeperFrame());
        super.setVisible(b);
    }
}